var t = require;
var e = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var a = (function () {
    function t() {}
    t.splitPolygonByTriangle = function (t) {
        if (t.length <= 3) return [0, 1, 2];
        for (var e = {}, o = 0; o < t.length; o++) {
            var a = t[o];
            e[a.x + "-" + a.y] = o;
        }
        var i = function (t) {
            return e[t.x + "-" + t.y];
        };
        t = t.concat([]);
        for (var n = [], r = 0; t.length > 3; ) {
            var s = t[r % t.length],
                c = t[(r + 1) % t.length],
                p = t[(r + 2) % t.length],
                l = (r + 1) % t.length,
                d = c.sub(s),
                u = p.sub(c);
            if (d.cross(u) < 0) r = (r + 1) % t.length;
            else {
                for (var h = !1, f = 0, y = t; f < y.length; f++)
                    if ((a = y[f]) != s && a != c && a != p && this.isInTriangle(a, s, c, p)) {
                        h = !0;
                        break;
                    }
                if (h) r = (r + 1) % t.length;
                else {
                    n.push(i(s), i(c), i(p));
                    t.splice(l, 1);
                }
            }
        }
        for (var v = 0, g = t; v < g.length; v++) {
            a = g[v];
            n.push(i(a));
        }
        return n;
    };
    t.isInTriangle = function (t, e, o, a) {
        var i = o.sub(e),
            n = a.sub(e),
            r = a.sub(o),
            s = t.sub(e),
            c = t.sub(o);
        return (
            (i.cross(n) >= 0) ^ (i.cross(s) < 0) &&
            (i.cross(n) >= 0) ^ (n.cross(s) >= 0) &&
            (r.cross(i) > 0) ^ (r.cross(c) >= 0)
        );
    };
    t.captureScreen = function (t, e) {
        var o = new cc.RenderTexture(),
            a = t.targetTexture,
            i = cc.rect(0, 0, cc.visibleRect.width, cc.visibleRect.height);
        e && (i = e instanceof cc.Node ? e.getBoundingBoxToWorld() : e);
        o.initWithSize(cc.visibleRect.width, cc.visibleRect.height, cc.game._renderContext.STENCIL_INDEX8);
        t.targetTexture = o;
        t.render();
        t.targetTexture = a;
        var n = new ArrayBuffer(i.width * i.height * 4),
            r = new Uint8Array(n);
        o.readPixels(r, i.x, i.y, i.width, i.height);
        return r;
    };
    t.computeUv = function (t, e, o) {
        for (var a = [], i = 0, n = t; i < n.length; i++) {
            var r = n[i],
                s = this.clamp(0, 1, (r.x + e / 2) / e),
                c = this.clamp(0, 1, 1 - (r.y + o / 2) / o);
            a.push(cc.v2(s, c));
        }
        return a;
    };
    t.clamp = function (t, e, o) {
        if (t > e) {
            var a = t;
            t = e;
            e = a;
        }
        return o < t ? t : o > e ? e : o;
    };
    t.isInPolygon = function (t, e) {
        var o,
            a,
            i,
            n,
            r = 0,
            s = e.length;
        i = e[0];
        for (o = 1; o <= s; o++) {
            n = e[o % s];
            if (t.x > Math.min(i.x, n.x) && t.x <= Math.max(i.x, n.x) && t.y <= Math.max(i.y, n.y) && i.x != n.x) {
                a = ((t.x - i.x) * (n.y - i.y)) / (n.x - i.x) + i.y;
                (i.y == n.y || t.y <= a) && r++;
            }
            i = n;
        }
        return r % 2 != 0;
    };
    t.lineCutPolygon = function (t, e, o) {
        for (var a = [], i = [], n = [], r = 0; r < o.length; ++r) {
            i.push(o[r]);
            var s = o[r],
                c = o[0];
            r < o.length - 1 && (c = o[r + 1]);
            var p = this.lineCrossPoint(t, e, s, c);
            if (0 == p[0]) {
                n.push(i.length);
                i.push(p[1]);
            } else p[0] > 0 && (p[1].equals(s) ? n.push(i.length - 1) : n.push(i.length));
        }
        if (n.length > 1) {
            var l = i[n[0]],
                d = i[n[1]],
                u = this.relationPointToPolygon(new cc.Vec2((l.x + d.x) / 2, (l.y + d.y) / 2), o),
                h = u >= 0,
                f = i[n[n.length - 1]];
            if (f && n.length > 2 && l.sub(d).mag() > l.sub(f).mag()) {
                d = i[n[n.length - 1]];
                h = (u = this.relationPointToPolygon(new cc.Vec2((l.x + d.x) / 2, (l.y + d.y) / 2), o)) < 0;
            }
            var y = h,
                v = 0,
                g = n[v],
                m = [],
                _ = [],
                C = 0;
            m.push(i[g]);
            h && _.push(i[g]);
            v++;
            C++;
            g++;
            for (; C < i.length; ) {
                g == i.length && (g = 0);
                var b = i[g];
                if (v >= 0 && g == n[v]) {
                    ++v >= n.length && (v = 0);
                    if (h) {
                        _.push(b);
                        a.push(_);
                        _ = [];
                    } else (_ = []).push(b);
                    m.push(b);
                    h = !h;
                } else h ? _.push(b) : m.push(b);
                g++;
                C++;
            }
            if (h)
                if (!y && _.length > 1) {
                    _.push(i[n[0]]);
                    a.push(_);
                } else for (r = 0; r < _.length; ++r) m.push(_[r]);
            a.push(m);
        }
        return a;
    };
    t.relationPointToPolygon = function (t, e) {
        for (var o = 0, a = 0; a < e.length; ++a) {
            if (e[a].equals(t)) return 2;
            var i = e[a],
                n = e[0];
            a < e.length - 1 && (n = e[a + 1]);
            var r = this.rayPointToLine(t, i, n);
            if (1 == r) return 1;
            0 == r && o++;
        }
        return o % 2 == 0 ? -1 : 0;
    };
    t.lineCrossPoint = function (t, e, o, a) {
        var i,
            n,
            r,
            s,
            c,
            p,
            l = t,
            d = e,
            u = o,
            h = a,
            f = new cc.Vec2(0, 0);
        r = this.dblcmp((i = this.ab_cross_ac(l, d, u)), 0);
        s = this.dblcmp((n = this.ab_cross_ac(l, d, h)), 0);
        c = this.dblcmp(this.ab_cross_ac(u, h, l), 0);
        p = this.dblcmp(this.ab_cross_ac(u, h, d), 0);
        if (-2 == (r ^ s) && -2 == (c ^ p)) {
            f.x = (u.x * n - h.x * i) / (n - i);
            f.y = (u.y * n - h.y * i) / (n - i);
            return [0, f];
        }
        return 0 == r && this.point_on_line(u, l, d) <= 0
            ? [1, (f = u)]
            : 0 == s && this.point_on_line(h, l, d) <= 0
            ? [1, (f = h)]
            : 0 == c && this.point_on_line(l, u, h) <= 0
            ? [1, (f = l)]
            : 0 == p && this.point_on_line(d, u, h) <= 0
            ? [1, (f = d)]
            : [-1, null];
    };
    t.point_on_line = function (t, e, o) {
        return this.dblcmp(this.dot(e.x - t.x, e.y - t.y, o.x - t.x, o.y - t.y), 0);
    };
    t.dblcmp = function (t, e) {
        return Math.abs(t - e) <= 1e-6 ? 0 : t > e ? 1 : -1;
    };
    t.ab_cross_ac = function (t, e, o) {
        return this.cross(e.x - t.x, e.y - t.y, o.x - t.x, o.y - t.y);
    };
    t.dot = function (t, e, o, a) {
        return t * o + e * a;
    };
    t.cross = function (t, e, o, a) {
        return t * a - o * e;
    };
    t.rayPointToLine = function (t, e, o) {
        Math.min(e.x, o.x);
        var a = Math.max(e.x, o.x),
            i = Math.min(e.y, o.y),
            n = Math.max(e.y, o.y);
        if (t.y < i || t.y > n || t.x > a) return -1;
        var r = e.x + ((o.x - e.x) / (o.y - e.y)) * (t.y - e.y);
        return r > t.x ? 0 : r == t.x ? 1 : -1;
    };
    return t;
})();
o.default = a;
